import { useUserStore } from '@/store/index.js'
// import useUserStore from '@/store/modules/user.js';
import { GetHomeGoodsListApi } from '@/service/index.js'
// 全局请求封装
export default(path, params = {}, loading = true, method = "POST") => {
	if (loading) uni.showLoading({ title: "加载中", mask: true });

	return new Promise((resolve, reject) => {
    const { token } = useUserStore()
		uni.request({
			url: import.meta.env.VITE_APP_BASE_URL + path,
			method,
			data: params,
      timeout: 15000,
			header:{ Authorization: token },
			success(res) {
				uni.hideLoading();
				const { statusCode, data } = res
				if (statusCode === 200) {
					if (data.code == 1) {
						resolve(data.data);
					} else if(data.code == 401) {
            getApp().globalData.toast(data.msg)
						uni.navigateTo({ url: '/pages/login/login' })
					} else {
						if(path == '/system/sms/send_sms') reject(data)
						getApp().globalData.toast(data.msg)
						reject(data)
					}
				} else {
					reject(res)
				}
			},
			fail(err) {
				uni.hideLoading();
				getApp().globalData.toast(data.msg)
				reject(err);
			}
		});
	});
};